﻿using MyDAL.model;
using MyDAL.helper;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Web.Services.Description;

namespace WebApplication1
{
    public partial class JobAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            initCompany();
        }

        private void initCompany()
        {
            DropDownList3.Items.Clear();
            CheckBoxList1.Items.Clear();
            foreach (var item in DataHelper.GetInstance().JobCompanytList)
            {
                DropDownList3.Items.Add(item);
                CheckBoxList1.Items.Add(item);
            }
            foreach (var item in DataHelper.GetInstance().JobStrConfig.JobJCWTD)
            {
                CheckBoxList2.Items.Add(item);
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text.Trim();
            ShiGongTianChuangModel job = new ShiGongTianChuangModel();
            job.JobName = TextBox1.Text.Trim();
            job.JobAddress = DropDownList2.Text.Trim();
            job.JobNature = TextBox2.Text.Trim();
            job.JobLYCPH = RadioButton1.Checked;
            job.JobSubjectCompany = DropDownList3.Text.Trim();
            job.JobObjectCompany = new List<string>();
            //string i = CheckBoxList1.SelectedValue;
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    job.JobObjectCompany.Add(CheckBoxList1.Items[i].Value);
                }
            }
            job.JobJCWTD = new List<string>();
            for (int i = 0; i < CheckBoxList2.Items.Count; i++)
            {
                if (CheckBoxList2.Items[i].Selected)
                {
                    job.JobJCWTD.Add(CheckBoxList2.Items[i].Value);
                }
            }

            if (DropDownList1.Text.Trim() == "天窗")
            {
                job.JobIndex = GuidTo16String();
            }
            else
            {
                job.JobIndex = TextBox2.Text.Trim();
            }

            //添加流程
            job.JobTechnologicalProcess = new List<string>();
            job.JobTechnologicalProcess.Add("车站人员盯控签认");
            job.JobTechnologicalProcess.Add("登记内容确认");
            job.JobTechnologicalProcess.Add("开始");
            job.JobTechnologicalProcess.Add("销记确认");
            job.JobTechnologicalProcess.Add("结束");
            //添加流程色
            //String color = ColorTranslator.ToHtml(Color.LightGreen);
            job.JobTProcessEnabled = new List<bool>();
            job.JobTProcessEnabled.Add(true);
            job.JobTProcessEnabled.Add(true);
            job.JobTProcessEnabled.Add(true);
            job.JobTProcessEnabled.Add(true);
            job.JobTProcessEnabled.Add(true);

            DataHelper.GetInstance().JobList.Add(job);
            bool isOk = DataHelper.GetInstance().refrashJobList();
            //bool isOk = XmlHelper.ObjectToXMLFile(DataHelper.GetInstance().JobList, ".\\JobModelList.xml", Encoding.Unicode);

            if (isOk)
            {
                Response.Write("<script>alert('添加成功');window.location.href=window.location.href;</script>");
                //Message.Show("添加成功");
            }
            else
            {
                Response.Write("<script>alert('添加失败')</script>");
                //MessageBox.Show("添加失败");
            }
            //Response.Redirect("WebFormMain.aspx");
        }

        /// <summary>  
        /// 根据GUID获取16位的唯一字符串  
        /// </summary>  
        /// <param name=\"guid\"></param>  
        /// <returns></returns>  
        public static string GuidTo16String()
        {
            long i = 1;
            foreach (byte b in Guid.NewGuid().ToByteArray())
                i *= ((int)b + 1);
            return string.Format("{0:x}", i - DateTime.Now.Ticks);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            DataHelper.GetInstance().JobCompanytList.Add(TextBox3.Text.Trim());
            initCompany();
            DataHelper.GetInstance().refrashJobCompany();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Response.Redirect("WebFormMain.aspx");
        }
    }
}